package icu.sehnen.service.impl;

import icu.sehnen.mapper.MenusMapper;
import icu.sehnen.model.Menus;
import icu.sehnen.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class MenusServiceImpl implements MenusService {

    @Autowired
     MenusMapper menusMapper;

    @Override
    public  List<Menus> queryMenuList() {
        // 原始的数据一条一条的
        List<Menus> rootMenu = menusMapper.findAll();

        // 构建好的菜单树，第一层菜单的pid是null

        return buildMenuTree(rootMenu,0L);
    }

    private List<Menus> buildMenuTree(List<Menus> menuList, Long pid) {
        List<Menus> treeList = new ArrayList<>();
        menuList.forEach(menu -> {
            if (pid.equals(menu.getPid())) {
                menu.setChildMenu(buildMenuTree(menuList, menu.getId()));
                treeList.add(menu);
            }
        });
        return treeList;
    }


}
